package com.yanqu.road.server.cmd.activity.unionchest.cross;

import com.yanqu.road.entity.activity.unionChest.config.UnionChestBoxConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogUnionChest;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.UnionChestProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.unionchest.UnionChestMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Date;

@Cmd(code = Protocol.S_UNIONCHEST_BOX_OPEN_CROSS_BACK, desc = "开宝箱,跨服返回")
public class CrossUnionChestOpenBackCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        UnionChestProto.CrossUnionChestOpenBoxRespMsg respMsg = UnionChestProto.CrossUnionChestOpenBoxRespMsg.parseFrom(packet.getBytes());
        if (null != player) {
            player.sendPacket(ClientProtocol.U_UNIONCHEST_BOX_OPEN, respMsg.getRespMsg().toBuilder());
        }
        if (0 == respMsg.getRespMsg().getRet()) {//广播给商会成员
            Property reward = PropertyHelper.parseStringToProperty(respMsg.getRespMsg().getReward());
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.UnionChest, eLogMoneyType.UnionChestOpen);

            UnionChestProto.UnionChestBoxSyncMsg.Builder syncMsg = UnionChestProto.UnionChestBoxSyncMsg.newBuilder();
            syncMsg.setActivityId(respMsg.getRespMsg().getActivityId());
            syncMsg.addBoxList(respMsg.getRespMsg().getBox());
            UnionChestProto.CrossUnionChestBoxSyncMsg.Builder crossMsg = UnionChestProto.CrossUnionChestBoxSyncMsg.newBuilder();
            crossMsg.setUnionId(respMsg.getUnionId());
            crossMsg.setSyncMsg(syncMsg);
            YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C3_UNIONCHEST_BOX_SYNC, crossMsg);
            GamePlayerMgr.sendPacket(respMsg.getUserId(), message);

            int boxGoodsId = UnionChestMgr.getBoxGoodsId(respMsg.getRespMsg().getBox().getBoxType());
            boolean isBigReward = UnionChestMgr.isBigReward(boxGoodsId, reward);
            if (isBigReward) {
                UnionChestProto.CrossUnionChestNoticeAddMsg.Builder addNoticeMsg = UnionChestProto.CrossUnionChestNoticeAddMsg.newBuilder();
                addNoticeMsg.setActivityId(UnionChestMgr.getActivityInfo().getActivityId());
                addNoticeMsg.setUnionId(respMsg.getUnionId());
                addNoticeMsg.setBoxId(respMsg.getRespMsg().getBox().getBoxId());
                addNoticeMsg.setBoxIndex(respMsg.getBoxIndex());
                GamePlayerMgr.sendPacket(respMsg.getUserId(), YanQuMessageUtils.buildMessage(CrossProtocol.C2_UNIONCHEST_BOX_NOTICE_ADD, addNoticeMsg));
            }

            AutoLogMgr.add(new LogUnionChest(respMsg.getRespMsg().getActivityId(), respMsg.getUserId(), respMsg.getUnionId(),
                    respMsg.getRespMsg().getBox().getBoxId(),
                    respMsg.getRespMsg().getBox().getBoxType(),
                    boxGoodsId,
                    respMsg.getBoxIndex(),
                    respMsg.getRespMsg().getBox().getShareUserId(),
                    respMsg.getRespMsg().getReward(),
                    new Date(respMsg.getRespMsg().getBox().getShareTime())
            ));
        }
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
